package com.xinghou.platform.dao.main.impl;

import java.lang.reflect.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.xinghou.platform.dao.bases.redis.RedisOperator;
import com.xinghou.platform.dao.main.common.DBUserLevelCached;
import com.xinghou.platform.util.DateUtil;
import com.xinghou.platform.util.JsonUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.VersionConfigDAO;
import com.xinghou.platform.model.main.VersionConfig;

@Service
public class VersionConfigDAOImpl  {/*
    private static String MAJIA_ACCOUNT_KEY = "majia_account_key";
    private static String MESSAGE_NOTIFY_KEY = "message_notify_key:%s";
    private static String YY_ACCOUNT_LOGINTIME = "yy_account_logintime:%s";

    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private RedisOperator redisOperator;

    @Override
    public VersionConfig getVersionConfigByOsType(byte osType) throws DataAccessException {
        CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
        String sql = "SELECT * FROM version_config WHERE Os_type = ?";
        VersionConfig versionConfig = jdbcTemplate.queryForObject(sql, new Object[]{osType}, new VersionConfigMapper());
        CustomerContextHolder.clearCustomerType();
        return versionConfig;
    }

    @Override
    public int updateVersionConfigByOsType(VersionConfig versionConfig) {

        StringBuffer strSQL = new StringBuffer("UPDATE version_config set Url = ?,")
                .append(" Version= ?, Change_log = ?, Create_time = ? where Os_type=?");

        Object[] params = {versionConfig.getUrl(), versionConfig.getVersion(),
                versionConfig.getChangeLog(), new Date(), versionConfig.getOsType()
        };
        CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
        int ret = jdbcTemplate.update(strSQL.toString(), params);
        CustomerContextHolder.clearCustomerType();
        updateViersionConfigRedis(versionConfig);
        return ret;
    }



    public void recordYyLogin(String yyid) {
        String key = String.format(YY_ACCOUNT_LOGINTIME, yyid);
        Long time = DateUtil.getTimeStampByDate(new Date());
        redisOperator.set(key, time.toString(), DBUserLevelCached.CACHED_DB_INDEX);
    }

    public Long getRecentYyLoginTime(String yyid) {
        String key = String.format(YY_ACCOUNT_LOGINTIME, yyid);
        String time = redisOperator.get(key, DBUserLevelCached.CACHED_DB_INDEX);
        if (time != null)
            return Long.parseLong(time);
        return 0l;
    }


    public List<String> filterOffLineAccount(List<String> accounts) {
        List<String> temp = new ArrayList<String>();
        if (accounts == null || accounts.size() == 0)
            return null;
        for (int i = 0; i < accounts.size() - 1; i++) {
            Long current = DateUtil.getTimeStampByDate(new Date());
            if (current - getRecentYyLoginTime(accounts.get(i)) < 5 * 60 * 1000) {
                temp.add(accounts.get(i));
            }
        }
        return temp;

    }


    public void updateViersionConfigRedis(VersionConfig versionConfig) {
        redisOperator.set(String.format(DBUserLevelCached.VERSION_CONFIG_OSTYPE, versionConfig.getOsType()), JsonUtil.toJson(versionConfig), DBUserLevelCached.CACHED_DB_INDEX);
    }


    private static final class VersionConfigMapper implements RowMapper<VersionConfig> {
        public VersionConfig mapRow(ResultSet rs, int rowNum) throws SQLException {
            VersionConfig versionConfig = new VersionConfig();
            versionConfig.setId(rs.getInt("Id"));
            versionConfig.setChangeLog(rs.getString("Change_log"));
            versionConfig.setOsType(rs.getInt("Os_type"));
            versionConfig.setUrl(rs.getString("Url"));
            versionConfig.setVersion(rs.getString("Version"));
            versionConfig.setCreateTime(rs.getTimestamp("Create_time"));
            versionConfig.setIsforce(rs.getInt("isforce"));
            return versionConfig;
        }
    }*/

}
